package br.com.pesca.converter;

import br.com.pesca.dao.HabitatDAO;
import br.com.pesca.entity.Habitat;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.convert.Converter;

public class HabitatConverter implements Converter {

    private final HabitatDAO habitatDAO = new HabitatDAO();

    @Override
    public Object getAsObject(FacesContext fc, UIComponent uic, String string) {
        if (string.equals("vazio")) {
            return new Habitat();
        } else {
            habitatDAO.startOperation();
            Object obj = habitatDAO.find(Habitat.class, Long.parseLong(string));
            habitatDAO.stopOperation(true);
            return obj;
        }
    }

    @Override
    public String getAsString(FacesContext fc, UIComponent uic, Object o) {
        if (o.equals("vazio")) {
            return "vazio";
        } else {
            return String.valueOf(((Habitat) o).getId());
        }
    }

}
